//
//  ALPS_TSAllDViewController.m
//  Alps
//
//  Created by dllo on 16/6/16.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "ALPS_TSAllDViewController.h"
#import "ALPS_TSearchAllTCell.h"
#import "ALPS_TSearchAllDModel.h"
#import "ALPS_ClassifyClickClickAlbumController.h"
#import "ALPS_PlayerMainViewController.h"
#import "ALPS_TIDListModel.h"
@interface ALPS_TSAllDViewController ()<UITableViewDelegate , UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;
//专辑的数组
@property (nonatomic, strong) NSMutableArray *albumArray;
//声音的数组
@property (nonatomic, strong) NSMutableArray *voiceArray;

//记录总共有多少专辑
@property (nonatomic, copy) NSString *albumString;
//记录总共有多少首声音
@property (nonatomic, copy) NSString *voiceString;

//构造id数组
@property (nonatomic, strong) NSMutableArray *idArray;
@end

@implementation ALPS_TSAllDViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor cyanColor];
    self.albumArray = [NSMutableArray array];
    self.voiceArray = [NSMutableArray array];
    [self getView];
    [self getData];
    
}


-(void)getView
{
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, ScreeWidth, ScreeHeight - 64 - 36) style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.separatorStyle = UITableViewCellSelectionStyleNone;
    [self.view addSubview:_tableView];
    [self.tableView registerClass:[ALPS_TSearchAllTCell class] forCellReuseIdentifier:@"cell"];
    

}

-(void)getData
{
    self.idArray = [NSMutableArray array];
    [WSSNetTool GetNetByURL:[NSString stringWithFormat:@"http://search.ximalaya.com/front/v1?core=all&device=iPhone&kw=%@&live=true&page=1&paidFilter=false&rows=3&spellchecker=true", self.searchText] body:nil headers:nil response:0 success:^(id result) {
        //专辑
        NSDictionary *dic = [result objectForKey:@"album"];
        self.albumString = [dic objectForKey:@"numFound"];
        NSArray *array = [dic objectForKey:@"docs"];
        for (NSDictionary *dict in array) {
            ALPS_TSearchAllDModel *model = [[ALPS_TSearchAllDModel alloc] initWithDic:dict];
            [self.albumArray addObject:model];
        }
        
        //声音
        NSDictionary *dicV = [result objectForKey:@"track"];
        self.voiceString = [dicV objectForKey:@"numFound"];
        NSArray *arr = [dicV objectForKey:@"docs"];
        for (NSDictionary *dictV in arr) {
            ALPS_TSearchAllDModel *model = [[ALPS_TSearchAllDModel alloc] initWithDic:dictV];
            [self.voiceArray addObject:model];
            ALPS_TIDListModel *dataModel = [[ALPS_TIDListModel alloc] init];
            dataModel.trackId = [[dictV objectForKey:@"id"] integerValue];
            dataModel.title = [dictV objectForKey:@"title"];
            dataModel.coverLarge = [dictV objectForKey:@"cover_path"];
            [self.idArray addObject:dataModel];
        }
        
        [self.tableView reloadData];
    } failure:^(NSError *error) {
        
    }];
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    return self.albumArray.count * 100 + 50;
}



-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 2;
}


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
        
        ALPS_TSearchAllTCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
        cell.dataSource = self.albumArray;
        cell.selectionStyle = UITableViewCellEditingStyleNone;
        NSString *string = [NSString stringWithFormat:@"全部%@张专辑", self.albumString];
        if (self.albumString != NULL) {
            NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:string];
            NSRange rang = [string rangeOfString:[NSString stringWithFormat:@"%@", self.albumString]];
            [attributedString setAttributes:[NSMutableDictionary dictionaryWithObjectsAndKeys:[UIColor orangeColor], NSForegroundColorAttributeName, nil] range:rang];
            cell.introduceLabel.attributedText = attributedString;
        }
        cell.type = 1;
        cell.jump = ^(NSInteger num)
        {
            if (num >= 0) {
            ALPS_TSearchAllDModel *model = [self.albumArray objectAtIndex:num];
                ALPS_ClassifyClickClickAlbumController *cccVC = [[ALPS_ClassifyClickClickAlbumController alloc] init];
                cccVC.albumId = model.id;
                [self.navigationController pushViewController:cccVC animated:YES];
            }
            
        };
        return cell;
    }
    else
    {
        
        ALPS_TSearchAllTCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
        cell.dataSource = self.voiceArray;
        cell.selectionStyle = UITableViewCellEditingStyleNone;
        NSString *string = [NSString stringWithFormat:@"全部%@首声音", self.voiceString];
        if (self.voiceString != NULL) {
            NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:string];
            NSRange rang = [string rangeOfString:[NSString stringWithFormat:@"%@", self.voiceString]];
            [attributedString setAttributes:[NSMutableDictionary dictionaryWithObjectsAndKeys:[UIColor orangeColor], NSForegroundColorAttributeName, nil] range:rang];
            cell.introduceLabel.attributedText = attributedString;
            
        }
        cell.titleLabel.text = @"声音";
        cell.jump = ^(NSInteger num)
        {
            if (num >= 0) {
                ALPS_TSearchAllDModel *model = [self.voiceArray objectAtIndex:num];
                ALPS_PlayerMainViewController *pmVC = [[ALPS_PlayerMainViewController alloc] init];
                pmVC.trackId = model.id;
                pmVC.listArr = self.idArray;
                [self.navigationController pushViewController:pmVC animated:YES];
            }
            
        };

        return cell;
    }
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
        self.mobile(indexPath.row);
    
}



//检测是否上下滚动方便回收键盘
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    self.Value(1);
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
